package com.wenjie.common.utils.check;

import org.apache.logging.log4j.util.Strings;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author wenjie
 * @Date 2022/10/21
 * @Description: 用来验证各种信息
 */
public class CheckUtil {
    /**
     * @Description: 验证是否是手机号
     * @Param:
     * @Return:
     * @Author: wenjie
     * @Date: 2022/10/21
     */
    public static boolean checkPhone(CharSequence phoneNumber) {
        return PhoneUtil.checkPhone(phoneNumber.toString());
    }


    /**
     * @Description: 验证是否是邮箱地址
     * @Param:
     * @Return:
     * @Author: wenjie
     * @Date: 2022/10/21
     */
    public static boolean checkEmail(String email) {
        if (email == null || email.length() < 1 || email.length() > 256) {
            return false;
        }
        Pattern pattern = Pattern.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
        return pattern.matcher(email).matches();
    }

    /**
     * @Description: 验证是否是url
     * @Param:
     * @Return:
     * @Author: wenjie
     * @Date: 2022/10/21
     */
    public static boolean checkUrl(String url) {
        if (Strings.isBlank(url)) return false;
        //转换为小写
        url = url.toLowerCase();
        String regex = "^((https|http|ftp|rtsp|mms)?://)"  //https、http、ftp、rtsp、mms
                + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //ftp的user@
                + "(([0-9]{1,3}\\.){3}[0-9]{1,3}" // IP形式的URL- 例如：199.194.52.184
                + "|" // 允许IP和DOMAIN（域名）
                + "([0-9a-z_!~*'()-]+\\.)*" // 域名- www.
                + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\\." // 二级域名
                + "[a-z]{2,6})" // first level domain- .com or .museum
                + "(:[0-9]{1,5})?" // 端口号最大为65535,5位数
                + "((/?)|" // a slash isn't required if there is no file name
                + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
        Pattern pattern = Pattern.compile(regex);
        return pattern.matcher(url).matches();
    }

    /**
     * @Description: 验证是否是时间字符串
     * @Param:
     * @Return:
     * @Author: wenjie
     * @Date: 2022/10/21
     */
    public static boolean checkDataStr(String dataStr) {
        return false;
    }

    /**
     * @Description: 验证是否是车牌号
     * @Param:
     * @Return:
     * @Author: wenjie
     * @Date: 2022/10/21
     */
    public static boolean checkPlateNum(String chardNum) {
        if (chardNum.length() >= 7 && chardNum.length() <= 8) {
            Pattern p = Pattern.compile("^([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[a-zA-Z](([DF]((?![IO])[a-zA-Z0-9](?![IO]))[0-9]{4})|([0-9]{5}[DF]))|[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1})$");
            Matcher m = p.matcher(chardNum);
            return m.matches();
        } else {
            return false;
        }
    }

    /**
     * @Description: 验证是否是车架号
     * @Param:
     * @Return:
     * @Author: wenjie
     * @Date: 2022/10/21
     */
    public static boolean checkVin(String vin) {
        return VinUtil.isVin(vin);
    }


    /**
     * @Description: 验证是否是银行卡号
     * @Param bankCard
     * @Return:
     * @Author: wenjie
     * @Date: 2022/10/21
     */
    public static boolean checkBankCard(String bankCard) {
        if (bankCard.length() < 15 || bankCard.length() > 19) {
            return false;
        }
        char bit = getBankCardCheckCode(bankCard.substring(0, bankCard.length() - 1));
        if (bit == 'N') {
            return false;
        }
        return bankCard.charAt(bankCard.length() - 1) == bit;
    }

    /**
     * 从不含校验位的银行卡卡号采用 Luhm 校验算法获得校验位
     *
     * @param nonCheckCodeBankCard
     * @return
     */
    public static char getBankCardCheckCode(String nonCheckCodeBankCard) {
        if (nonCheckCodeBankCard == null || nonCheckCodeBankCard.trim().length() == 0
                || !nonCheckCodeBankCard.matches("\\d+")) {
            //如果传的不是数据返回N
            return 'N';
        }
        char[] chs = nonCheckCodeBankCard.trim().toCharArray();
        int luhmSum = 0;
        for (int i = chs.length - 1, j = 0; i >= 0; i--, j++) {
            int k = chs[i] - '0';
            if (j % 2 == 0) {
                k *= 2;
                k = k / 10 + k % 10;
            }
            luhmSum += k;
        }
        return (luhmSum % 10 == 0) ? '0' : (char) ((10 - luhmSum % 10) + '0');
    }

    /**
     * @Description: 验证是否是身份证号
     * @Param:
     * @Return:
     * @Author: wenjie
     * @Date: 2022/10/21
     */
    public static boolean checkIdChardNum(String IdchardNum) {
        return IdChardUtil.judgeId(IdchardNum);
    }

    /**
     * @Description: 验证是否是营业执照 统一社会信用代码
     * @Param:
     * @Return:
     * @Author: wenjie
     * @Date: 2022/10/21
     */
    public static boolean checkBL(String businessLicense) {
        int length = businessLicense.length();
        boolean bool = false;
        switch (length) {
            case 15:
                bool = BusinessLicenseUtil.isLicense15(businessLicense);
                break;
            case 18:
                bool = BusinessLicenseUtil.isLicense18(businessLicense);
                break;
        }
        return bool;
    }


    /**
     * @Description: TODO 校验短信验证码
     * @Param:
     * @Return:
     * @Author: wenjie
     * @Date: 2023/1/30
     */
    public static void VerificationCode(String ide, String tel) {


    }
}
